#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

#define MAX_N 500000
int arr[MAX_N + 5], tmp[MAX_N + 5];

long long merge_sort(int*arr, int l, int r) {
	if (l >= r) return 0;
	int mid = (l + r) >> 1;

	long long res =merge_sort(arr, l, mid) + merge_sort(arr, mid + 1, r);
	int k = 0, i = l, j = mid + 1;
	while (i <= mid && j <= r) {
		if (arr[i] <= arr[j]) tmp[k++] = arr[i++];
		else {
			tmp[k++] = arr[j++];
			res += mid - i + 1;
		}
	}
	while (i <= mid)tmp[k++] = arr[i++];
	while (j <= r) tmp[k++] = arr[j++];

	for (i = l, j = 0; i <= r; i++, j++) arr[i] = tmp[j];
	return res;
}

void solve(int n) {
	for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
	printf("%lld\n", merge_sort(arr, 0, n - 1));
	return;
}


int main()
{

	int n;
	while (1)
	{
		scanf("%d", &n);
		if (n == 0) break;
		solve(n);
	}
	return 0;
}